<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>rect.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_math_rect.js.html">rect.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2006 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview A utility class for representing rectangles.
<a name="line17"></a> */
<a name="line18"></a>
<a name="line19"></a>
<a name="line20"></a>goog.provide(&#39;goog.math.Rect&#39;);
<a name="line21"></a>
<a name="line22"></a>goog.require(&#39;goog.math.Box&#39;);
<a name="line23"></a>goog.require(&#39;goog.math.Coordinate&#39;);
<a name="line24"></a>goog.require(&#39;goog.math.Size&#39;);
<a name="line25"></a>
<a name="line26"></a>
<a name="line27"></a>
<a name="line28"></a>/**
<a name="line29"></a> * Class for representing rectangular regions.
<a name="line30"></a> * @param {number} x Left.
<a name="line31"></a> * @param {number} y Top.
<a name="line32"></a> * @param {number} w Width.
<a name="line33"></a> * @param {number} h Height.
<a name="line34"></a> * @constructor
<a name="line35"></a> */
<a name="line36"></a>goog.math.Rect = function(x, y, w, h) {
<a name="line37"></a>  /**
<a name="line38"></a>   * Left
<a name="line39"></a>   * @type {number}
<a name="line40"></a>   */
<a name="line41"></a>  this.left = x;
<a name="line42"></a>
<a name="line43"></a>  /**
<a name="line44"></a>   * Top
<a name="line45"></a>   * @type {number}
<a name="line46"></a>   */
<a name="line47"></a>  this.top = y;
<a name="line48"></a>
<a name="line49"></a>  /**
<a name="line50"></a>   * Width
<a name="line51"></a>   * @type {number}
<a name="line52"></a>   */
<a name="line53"></a>  this.width = w;
<a name="line54"></a>
<a name="line55"></a>  /**
<a name="line56"></a>   * Height
<a name="line57"></a>   * @type {number}
<a name="line58"></a>   */
<a name="line59"></a>  this.height = h;
<a name="line60"></a>};
<a name="line61"></a>
<a name="line62"></a>
<a name="line63"></a>/**
<a name="line64"></a> * Returns a new copy of the rectangle.
<a name="line65"></a> * @return {!goog.math.Rect} A clone of this Rectangle.
<a name="line66"></a> */
<a name="line67"></a>goog.math.Rect.prototype.clone = function() {
<a name="line68"></a>  return new goog.math.Rect(this.left, this.top, this.width, this.height);
<a name="line69"></a>};
<a name="line70"></a>
<a name="line71"></a>
<a name="line72"></a>/**
<a name="line73"></a> * Returns a new Box object with the same position and dimensions as this
<a name="line74"></a> * rectangle.
<a name="line75"></a> * @return {!goog.math.Box} A new Box representation of this Rectangle.
<a name="line76"></a> */
<a name="line77"></a>goog.math.Rect.prototype.toBox = function() {
<a name="line78"></a>  var right = this.left + this.width;
<a name="line79"></a>  var bottom = this.top + this.height;
<a name="line80"></a>  return new goog.math.Box(this.top,
<a name="line81"></a>                           right,
<a name="line82"></a>                           bottom,
<a name="line83"></a>                           this.left);
<a name="line84"></a>};
<a name="line85"></a>
<a name="line86"></a>
<a name="line87"></a>/**
<a name="line88"></a> * Creates a new Rect object with the same position and dimensions as a given
<a name="line89"></a> * Box.  Note that this is only the inverse of toBox if left/top are defined.
<a name="line90"></a> * @param {goog.math.Box} box A box.
<a name="line91"></a> * @return {!goog.math.Rect} A new Rect initialized with the box&#39;s position
<a name="line92"></a> *     and size.
<a name="line93"></a> */
<a name="line94"></a>goog.math.Rect.createFromBox = function(box) {
<a name="line95"></a>  return new goog.math.Rect(box.left, box.top,
<a name="line96"></a>      box.right - box.left, box.bottom - box.top);
<a name="line97"></a>};
<a name="line98"></a>
<a name="line99"></a>
<a name="line100"></a>if (goog.DEBUG) {
<a name="line101"></a>  /**
<a name="line102"></a>   * Returns a nice string representing size and dimensions of rectangle.
<a name="line103"></a>   * @return {string} In the form (50, 73 - 75w x 25h).
<a name="line104"></a>   * @override
<a name="line105"></a>   */
<a name="line106"></a>  goog.math.Rect.prototype.toString = function() {
<a name="line107"></a>    return &#39;(&#39; + this.left + &#39;, &#39; + this.top + &#39; - &#39; + this.width + &#39;w x &#39; +
<a name="line108"></a>           this.height + &#39;h)&#39;;
<a name="line109"></a>  };
<a name="line110"></a>}
<a name="line111"></a>
<a name="line112"></a>
<a name="line113"></a>/**
<a name="line114"></a> * Compares rectangles for equality.
<a name="line115"></a> * @param {goog.math.Rect} a A Rectangle.
<a name="line116"></a> * @param {goog.math.Rect} b A Rectangle.
<a name="line117"></a> * @return {boolean} True iff the rectangles have the same left, top, width,
<a name="line118"></a> *     and height, or if both are null.
<a name="line119"></a> */
<a name="line120"></a>goog.math.Rect.equals = function(a, b) {
<a name="line121"></a>  if (a == b) {
<a name="line122"></a>    return true;
<a name="line123"></a>  }
<a name="line124"></a>  if (!a || !b) {
<a name="line125"></a>    return false;
<a name="line126"></a>  }
<a name="line127"></a>  return a.left == b.left &amp;&amp; a.width == b.width &amp;&amp;
<a name="line128"></a>         a.top == b.top &amp;&amp; a.height == b.height;
<a name="line129"></a>};
<a name="line130"></a>
<a name="line131"></a>
<a name="line132"></a>/**
<a name="line133"></a> * Computes the intersection of this rectangle and the rectangle parameter.  If
<a name="line134"></a> * there is no intersection, returns false and leaves this rectangle as is.
<a name="line135"></a> * @param {goog.math.Rect} rect A Rectangle.
<a name="line136"></a> * @return {boolean} True iff this rectangle intersects with the parameter.
<a name="line137"></a> */
<a name="line138"></a>goog.math.Rect.prototype.intersection = function(rect) {
<a name="line139"></a>  var x0 = Math.max(this.left, rect.left);
<a name="line140"></a>  var x1 = Math.min(this.left + this.width, rect.left + rect.width);
<a name="line141"></a>
<a name="line142"></a>  if (x0 &lt;= x1) {
<a name="line143"></a>    var y0 = Math.max(this.top, rect.top);
<a name="line144"></a>    var y1 = Math.min(this.top + this.height, rect.top + rect.height);
<a name="line145"></a>
<a name="line146"></a>    if (y0 &lt;= y1) {
<a name="line147"></a>      this.left = x0;
<a name="line148"></a>      this.top = y0;
<a name="line149"></a>      this.width = x1 - x0;
<a name="line150"></a>      this.height = y1 - y0;
<a name="line151"></a>
<a name="line152"></a>      return true;
<a name="line153"></a>    }
<a name="line154"></a>  }
<a name="line155"></a>  return false;
<a name="line156"></a>};
<a name="line157"></a>
<a name="line158"></a>
<a name="line159"></a>/**
<a name="line160"></a> * Returns the intersection of two rectangles. Two rectangles intersect if they
<a name="line161"></a> * touch at all, for example, two zero width and height rectangles would
<a name="line162"></a> * intersect if they had the same top and left.
<a name="line163"></a> * @param {goog.math.Rect} a A Rectangle.
<a name="line164"></a> * @param {goog.math.Rect} b A Rectangle.
<a name="line165"></a> * @return {goog.math.Rect} A new intersection rect (even if width and height
<a name="line166"></a> *     are 0), or null if there is no intersection.
<a name="line167"></a> */
<a name="line168"></a>goog.math.Rect.intersection = function(a, b) {
<a name="line169"></a>  // There is no nice way to do intersection via a clone, because any such
<a name="line170"></a>  // clone might be unnecessary if this function returns null.  So, we duplicate
<a name="line171"></a>  // code from above.
<a name="line172"></a>
<a name="line173"></a>  var x0 = Math.max(a.left, b.left);
<a name="line174"></a>  var x1 = Math.min(a.left + a.width, b.left + b.width);
<a name="line175"></a>
<a name="line176"></a>  if (x0 &lt;= x1) {
<a name="line177"></a>    var y0 = Math.max(a.top, b.top);
<a name="line178"></a>    var y1 = Math.min(a.top + a.height, b.top + b.height);
<a name="line179"></a>
<a name="line180"></a>    if (y0 &lt;= y1) {
<a name="line181"></a>      return new goog.math.Rect(x0, y0, x1 - x0, y1 - y0);
<a name="line182"></a>    }
<a name="line183"></a>  }
<a name="line184"></a>  return null;
<a name="line185"></a>};
<a name="line186"></a>
<a name="line187"></a>
<a name="line188"></a>/**
<a name="line189"></a> * Returns whether two rectangles intersect. Two rectangles intersect if they
<a name="line190"></a> * touch at all, for example, two zero width and height rectangles would
<a name="line191"></a> * intersect if they had the same top and left.
<a name="line192"></a> * @param {goog.math.Rect} a A Rectangle.
<a name="line193"></a> * @param {goog.math.Rect} b A Rectangle.
<a name="line194"></a> * @return {boolean} Whether a and b intersect.
<a name="line195"></a> */
<a name="line196"></a>goog.math.Rect.intersects = function(a, b) {
<a name="line197"></a>  return (a.left &lt;= b.left + b.width &amp;&amp; b.left &lt;= a.left + a.width &amp;&amp;
<a name="line198"></a>      a.top &lt;= b.top + b.height &amp;&amp; b.top &lt;= a.top + a.height);
<a name="line199"></a>};
<a name="line200"></a>
<a name="line201"></a>
<a name="line202"></a>/**
<a name="line203"></a> * Returns whether a rectangle intersects this rectangle.
<a name="line204"></a> * @param {goog.math.Rect} rect A rectangle.
<a name="line205"></a> * @return {boolean} Whether rect intersects this rectangle.
<a name="line206"></a> */
<a name="line207"></a>goog.math.Rect.prototype.intersects = function(rect) {
<a name="line208"></a>  return goog.math.Rect.intersects(this, rect);
<a name="line209"></a>};
<a name="line210"></a>
<a name="line211"></a>
<a name="line212"></a>/**
<a name="line213"></a> * Computes the difference regions between two rectangles. The return value is
<a name="line214"></a> * an array of 0 to 4 rectangles defining the remaining regions of the first
<a name="line215"></a> * rectangle after the second has been subtracted.
<a name="line216"></a> * @param {goog.math.Rect} a A Rectangle.
<a name="line217"></a> * @param {goog.math.Rect} b A Rectangle.
<a name="line218"></a> * @return {!Array.&lt;!goog.math.Rect&gt;} An array with 0 to 4 rectangles which
<a name="line219"></a> *     together define the difference area of rectangle a minus rectangle b.
<a name="line220"></a> */
<a name="line221"></a>goog.math.Rect.difference = function(a, b) {
<a name="line222"></a>  var intersection = goog.math.Rect.intersection(a, b);
<a name="line223"></a>  if (!intersection || !intersection.height || !intersection.width) {
<a name="line224"></a>    return [a.clone()];
<a name="line225"></a>  }
<a name="line226"></a>
<a name="line227"></a>  var result = [];
<a name="line228"></a>
<a name="line229"></a>  var top = a.top;
<a name="line230"></a>  var height = a.height;
<a name="line231"></a>
<a name="line232"></a>  var ar = a.left + a.width;
<a name="line233"></a>  var ab = a.top + a.height;
<a name="line234"></a>
<a name="line235"></a>  var br = b.left + b.width;
<a name="line236"></a>  var bb = b.top + b.height;
<a name="line237"></a>
<a name="line238"></a>  // Subtract off any area on top where A extends past B
<a name="line239"></a>  if (b.top &gt; a.top) {
<a name="line240"></a>    result.push(new goog.math.Rect(a.left, a.top, a.width, b.top - a.top));
<a name="line241"></a>    top = b.top;
<a name="line242"></a>    // If we&#39;re moving the top down, we also need to subtract the height diff.
<a name="line243"></a>    height -= b.top - a.top;
<a name="line244"></a>  }
<a name="line245"></a>  // Subtract off any area on bottom where A extends past B
<a name="line246"></a>  if (bb &lt; ab) {
<a name="line247"></a>    result.push(new goog.math.Rect(a.left, bb, a.width, ab - bb));
<a name="line248"></a>    height = bb - top;
<a name="line249"></a>  }
<a name="line250"></a>  // Subtract any area on left where A extends past B
<a name="line251"></a>  if (b.left &gt; a.left) {
<a name="line252"></a>    result.push(new goog.math.Rect(a.left, top, b.left - a.left, height));
<a name="line253"></a>  }
<a name="line254"></a>  // Subtract any area on right where A extends past B
<a name="line255"></a>  if (br &lt; ar) {
<a name="line256"></a>    result.push(new goog.math.Rect(br, top, ar - br, height));
<a name="line257"></a>  }
<a name="line258"></a>
<a name="line259"></a>  return result;
<a name="line260"></a>};
<a name="line261"></a>
<a name="line262"></a>
<a name="line263"></a>/**
<a name="line264"></a> * Computes the difference regions between this rectangle and {@code rect}. The
<a name="line265"></a> * return value is an array of 0 to 4 rectangles defining the remaining regions
<a name="line266"></a> * of this rectangle after the other has been subtracted.
<a name="line267"></a> * @param {goog.math.Rect} rect A Rectangle.
<a name="line268"></a> * @return {!Array.&lt;!goog.math.Rect&gt;} An array with 0 to 4 rectangles which
<a name="line269"></a> *     together define the difference area of rectangle a minus rectangle b.
<a name="line270"></a> */
<a name="line271"></a>goog.math.Rect.prototype.difference = function(rect) {
<a name="line272"></a>  return goog.math.Rect.difference(this, rect);
<a name="line273"></a>};
<a name="line274"></a>
<a name="line275"></a>
<a name="line276"></a>/**
<a name="line277"></a> * Expand this rectangle to also include the area of the given rectangle.
<a name="line278"></a> * @param {goog.math.Rect} rect The other rectangle.
<a name="line279"></a> */
<a name="line280"></a>goog.math.Rect.prototype.boundingRect = function(rect) {
<a name="line281"></a>  // We compute right and bottom before we change left and top below.
<a name="line282"></a>  var right = Math.max(this.left + this.width, rect.left + rect.width);
<a name="line283"></a>  var bottom = Math.max(this.top + this.height, rect.top + rect.height);
<a name="line284"></a>
<a name="line285"></a>  this.left = Math.min(this.left, rect.left);
<a name="line286"></a>  this.top = Math.min(this.top, rect.top);
<a name="line287"></a>
<a name="line288"></a>  this.width = right - this.left;
<a name="line289"></a>  this.height = bottom - this.top;
<a name="line290"></a>};
<a name="line291"></a>
<a name="line292"></a>
<a name="line293"></a>/**
<a name="line294"></a> * Returns a new rectangle which completely contains both input rectangles.
<a name="line295"></a> * @param {goog.math.Rect} a A rectangle.
<a name="line296"></a> * @param {goog.math.Rect} b A rectangle.
<a name="line297"></a> * @return {goog.math.Rect} A new bounding rect, or null if either rect is
<a name="line298"></a> *     null.
<a name="line299"></a> */
<a name="line300"></a>goog.math.Rect.boundingRect = function(a, b) {
<a name="line301"></a>  if (!a || !b) {
<a name="line302"></a>    return null;
<a name="line303"></a>  }
<a name="line304"></a>
<a name="line305"></a>  var clone = a.clone();
<a name="line306"></a>  clone.boundingRect(b);
<a name="line307"></a>
<a name="line308"></a>  return clone;
<a name="line309"></a>};
<a name="line310"></a>
<a name="line311"></a>
<a name="line312"></a>/**
<a name="line313"></a> * Tests whether this rectangle entirely contains another rectangle or
<a name="line314"></a> * coordinate.
<a name="line315"></a> *
<a name="line316"></a> * @param {goog.math.Rect|goog.math.Coordinate} another The rectangle or
<a name="line317"></a> *     coordinate to test for containment.
<a name="line318"></a> * @return {boolean} Whether this rectangle contains given rectangle or
<a name="line319"></a> *     coordinate.
<a name="line320"></a> */
<a name="line321"></a>goog.math.Rect.prototype.contains = function(another) {
<a name="line322"></a>  if (another instanceof goog.math.Rect) {
<a name="line323"></a>    return this.left &lt;= another.left &amp;&amp;
<a name="line324"></a>           this.left + this.width &gt;= another.left + another.width &amp;&amp;
<a name="line325"></a>           this.top &lt;= another.top &amp;&amp;
<a name="line326"></a>           this.top + this.height &gt;= another.top + another.height;
<a name="line327"></a>  } else { // (another instanceof goog.math.Coordinate)
<a name="line328"></a>    return another.x &gt;= this.left &amp;&amp;
<a name="line329"></a>           another.x &lt;= this.left + this.width &amp;&amp;
<a name="line330"></a>           another.y &gt;= this.top &amp;&amp;
<a name="line331"></a>           another.y &lt;= this.top + this.height;
<a name="line332"></a>  }
<a name="line333"></a>};
<a name="line334"></a>
<a name="line335"></a>
<a name="line336"></a>/**
<a name="line337"></a> * Returns the size of this rectangle.
<a name="line338"></a> * @return {!goog.math.Size} The size of this rectangle.
<a name="line339"></a> */
<a name="line340"></a>goog.math.Rect.prototype.getSize = function() {
<a name="line341"></a>  return new goog.math.Size(this.width, this.height);
<a name="line342"></a>};
<a name="line343"></a>
<a name="line344"></a>
<a name="line345"></a>/**
<a name="line346"></a> * Rounds the fields to the next larger integer values.
<a name="line347"></a> * @return {!goog.math.Rect} This rectangle with ceil&#39;d fields.
<a name="line348"></a> */
<a name="line349"></a>goog.math.Rect.prototype.ceil = function() {
<a name="line350"></a>  this.left = Math.ceil(this.left);
<a name="line351"></a>  this.top = Math.ceil(this.top);
<a name="line352"></a>  this.width = Math.ceil(this.width);
<a name="line353"></a>  this.height = Math.ceil(this.height);
<a name="line354"></a>  return this;
<a name="line355"></a>};
<a name="line356"></a>
<a name="line357"></a>
<a name="line358"></a>/**
<a name="line359"></a> * Rounds the fields to the next smaller integer values.
<a name="line360"></a> * @return {!goog.math.Rect} This rectangle with floored fields.
<a name="line361"></a> */
<a name="line362"></a>goog.math.Rect.prototype.floor = function() {
<a name="line363"></a>  this.left = Math.floor(this.left);
<a name="line364"></a>  this.top = Math.floor(this.top);
<a name="line365"></a>  this.width = Math.floor(this.width);
<a name="line366"></a>  this.height = Math.floor(this.height);
<a name="line367"></a>  return this;
<a name="line368"></a>};
<a name="line369"></a>
<a name="line370"></a>
<a name="line371"></a>/**
<a name="line372"></a> * Rounds the fields to nearest integer values.
<a name="line373"></a> * @return {!goog.math.Rect} This rectangle with rounded fields.
<a name="line374"></a> */
<a name="line375"></a>goog.math.Rect.prototype.round = function() {
<a name="line376"></a>  this.left = Math.round(this.left);
<a name="line377"></a>  this.top = Math.round(this.top);
<a name="line378"></a>  this.width = Math.round(this.width);
<a name="line379"></a>  this.height = Math.round(this.height);
<a name="line380"></a>  return this;
<a name="line381"></a>};
<a name="line382"></a>
<a name="line383"></a>
<a name="line384"></a>/**
<a name="line385"></a> * Translates this rectangle by the given offsets. If a
<a name="line386"></a> * {@code goog.math.Coordinate} is given, then the left and top values are
<a name="line387"></a> * translated by the coordinate&#39;s x and y values. Otherwise, top and left are
<a name="line388"></a> * translated by {@code tx} and {@code opt_ty} respectively.
<a name="line389"></a> * @param {number|goog.math.Coordinate} tx The value to translate left by or the
<a name="line390"></a> *     the coordinate to translate this rect by.
<a name="line391"></a> * @param {number=} opt_ty The value to translate top by.
<a name="line392"></a> * @return {!goog.math.Rect} This rectangle after translating.
<a name="line393"></a> */
<a name="line394"></a>goog.math.Rect.prototype.translate = function(tx, opt_ty) {
<a name="line395"></a>  if (tx instanceof goog.math.Coordinate) {
<a name="line396"></a>    this.left += tx.x;
<a name="line397"></a>    this.top += tx.y;
<a name="line398"></a>  } else {
<a name="line399"></a>    this.left += tx;
<a name="line400"></a>    if (goog.isNumber(opt_ty)) {
<a name="line401"></a>      this.top += opt_ty;
<a name="line402"></a>    }
<a name="line403"></a>  }
<a name="line404"></a>  return this;
<a name="line405"></a>};
<a name="line406"></a>
<a name="line407"></a>
<a name="line408"></a>/**
<a name="line409"></a> * Scales this rectangle by the given scale factors. The left and width values
<a name="line410"></a> * are scaled by {@code sx} and the top and height values are scaled by
<a name="line411"></a> * {@code opt_sy}.  If {@code opt_sy} is not given, then all fields are scaled
<a name="line412"></a> * by {@code sx}.
<a name="line413"></a> * @param {number} sx The scale factor to use for the x dimension.
<a name="line414"></a> * @param {number=} opt_sy The scale factor to use for the y dimension.
<a name="line415"></a> * @return {!goog.math.Rect} This rectangle after scaling.
<a name="line416"></a> */
<a name="line417"></a>goog.math.Rect.prototype.scale = function(sx, opt_sy) {
<a name="line418"></a>  var sy = goog.isNumber(opt_sy) ? opt_sy : sx;
<a name="line419"></a>  this.left *= sx;
<a name="line420"></a>  this.width *= sx;
<a name="line421"></a>  this.top *= sy;
<a name="line422"></a>  this.height *= sy;
<a name="line423"></a>  return this;
<a name="line424"></a>};
</pre>


</body>
</html>
